package com.cskaoyan.service.Impls;

import com.cskaoyan.bean.PageData1;
import com.cskaoyan.mapper.MarketRegionMapper;
import com.cskaoyan.model.MarketRegion;
import com.cskaoyan.service.RegionServlet;
import com.cskaoyan.util.MyBatisUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class RegionServletImpl implements RegionServlet {
    @Override
    public PageData1.Data data() {
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        MarketRegionMapper regionMapper = sqlSession.getMapper(MarketRegionMapper.class);
        //ctrl + alt + 左右 回到上一个操作的位置 或直接鼠标的前进后退
        List<MarketRegion> provinceList = regionMapper.selectByType((byte) 1);
        List<MarketRegion> cityList = regionMapper.selectByType((byte) 2);
        List<MarketRegion> areaList = regionMapper.selectByType((byte) 3);

        Map<Integer, List<MarketRegion>> cityIdAreaListMap = areaList.stream().collect(Collectors.groupingBy(MarketRegion::getPid));
        cityList.forEach(city -> city.setChildren(cityIdAreaListMap.get(city.getId())));

        Map<Integer, List<MarketRegion>> provinceIdCityListMap = cityList.stream().collect(Collectors.groupingBy(MarketRegion::getPid));
        provinceList.forEach(province -> province.setChildren(provinceIdCityListMap.get(province.getId())));

        // 封装查询出来的信息
        PageData1.Data data = new PageData1.Data();
        // 每页的数据量
        data.setLimit(provinceList.size());
        // 当前页的具体的数据
        data.setList(provinceList);
        // 当前页的页码
        data.setPage(1);
        // 总的页码
        data.setPages(1);
        // 总的数据量
        data.setTotal(provinceList.size());;

        sqlSession.close();
        return data;
    }
}
